/*
 * Copyright (C) 2014 Lucid Wolf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.lucidhex.io;

import com.lucidhex.util.Utilities;
import com.lucidhex.viewer.LucidIcon;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import java.util.HashMap;
import java.util.Iterator;

/**
 *  
 * @author Lucid Wolf
 */
class LucidIconConverter implements Converter{

    LucidIconConverter() {
        
    }

    @Override
    public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext mc) {
        
    }

    @Override
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext uc) {
        HashMap<String,String> attribMap = Utilities.createAttributeMapUpperCase(reader);
        LucidIcon li = new LucidIcon();
        String name = attribMap.get("NAME");
        if(name == null){
            name = "NOT SET";
        }
        reader.moveDown();
        String data = reader.getValue();
        reader.moveUp();
        li.loadSVGIcon(name, data);
        return li;
    }

    @Override
    public boolean canConvert(Class type) {
        return type.equals(LucidIcon.class);
    }

}
